<?php
declare (strict_types=1);

namespace app\controller;

use app\model\Reminder as ReminderModel;
use think\facade\Request;
use think\facade\Validate;


class Reminder
{
    // 创建提醒
    public function create()
    {
        $data = Request::post();

        // 验证请求参数
        $validate = Validate::rule([
            'user_id' => 'require|integer',
            'title' => 'require|max:255',
            'description' => 'max:1000',
            'reminder_time' => 'require|dateFormat:Y-m-d H:i:s',
            'task_id' => 'integer|exists:tasks,task_id', // 关联任务ID验证
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $reminder = new ReminderModel();
        $reminder->data([
            'user_id' => $data['user_id'],
            'title' => $data['title'],
            'description' => $data['description'] ?? '',
            'reminder_time' => $data['reminder_time'],
            'task_id' => $data['task_id'] ?? null,
        ]);

        if ($reminder->save()) {
            return json(['status' => 'success', 'message' => 'Reminder created successfully.']);
        } else {
            return json(['status' => 'error', 'message' => 'Failed to create reminder.']);
        }
    }

    // 获取用户的提醒列表
    public function list()
    {
        $userId = Request::get('user_id');

        if (!$userId) {
            return json(['status' => 'error', 'message' => 'User ID is required.']);
        }

        $reminders = ReminderModel::where('user_id', $userId)
            ->where('is_active', 1)
            ->with('task') // 关联任务数据
            ->order('reminder_time', 'asc')
            ->select();

        return json(['status' => 'success', 'data' => $reminders]);
    }

    // 删除提醒
    public function delete()
    {
        $reminderId = Request::post('reminder_id');

        if (!$reminderId) {
            return json(['status' => 'error', 'message' => 'Reminder ID is required.']);
        }

        $reminder = ReminderModel::find($reminderId);

        if ($reminder) {
            $reminder->is_active = 0; // 软删除，设置为无效
            $reminder->save();
            return json(['status' => 'success', 'message' => 'Reminder deleted successfully.']);
        } else {
            return json(['status' => 'error', 'message' => 'Reminder not found.']);
        }
    }

    // 更新提醒
    public function update()
    {
        $data = Request::post();

        // 验证请求参数
        $validate = Validate::rule([
            'reminder_id' => 'require|integer',
            'title' => 'max:255',
            'description' => 'max:1000',
            'reminder_time' => 'dateFormat:Y-m-d H:i:s',
            'task_id' => 'integer|exists:tasks,task_id', // 关联任务ID验证
            'is_active' => 'in:0,1',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $reminder = ReminderModel::find($data['reminder_id']);

        if ($reminder) {
            $reminder->data([
                'title' => $data['title'] ?? $reminder->title,
                'description' => $data['description'] ?? $reminder->description,
                'reminder_time' => $data['reminder_time'] ?? $reminder->reminder_time,
                'task_id' => $data['task_id'] ?? $reminder->task_id,
                'is_active' => $data['is_active'] ?? $reminder->is_active,
            ]);

            if ($reminder->save()) {
                return json(['status' => 'success', 'message' => 'Reminder updated successfully.']);
            } else {
                return json(['status' => 'error', 'message' => 'Failed to update reminder.']);
            }
        } else {
            return json(['status' => 'error', 'message' => 'Reminder not found.']);
        }
    }
}
